<template>
  <el-dialog
    :title="!dataForm.attrId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @closed="dialogClose"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="属性名" prop="attrName">
        <el-input v-model="dataForm.attrName" placeholder="属性名"></el-input>
      </el-form-item>

      <el-form-item label="属性图标" prop="icon">
        <el-input v-model="dataForm.icon" placeholder="属性图标"></el-input>
      </el-form-item>
      <el-form-item label="可选值列表" prop="valueSelect">
        <!-- <el-input v-model="dataForm.valueSelect" placeholder="可选值列表[用逗号分隔]"></el-input> -->
        <el-select
          v-model="dataForm.valueSelect"
          multiple
          filterable
          allow-create
          placeholder="请输入内容"
        ></el-select>
      </el-form-item>

      <el-form-item label="属性类型" prop="attrType">
        <el-select v-model="type" placeholder="请选择">
          <el-option label="规格参数" :value="1"></el-option>
          <el-option label="销售属性" :value="0"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="值类型" prop="valueType">
        <el-switch
          v-model="valueType"
          active-text="允许多个值"
          inactive-text="只能单个值"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :inactive-value="0"
          :active-value="1"
        ></el-switch>
      </el-form-item>

      <el-form-item label="所属分类" prop="catelogId">
        <category-cascader :catelogPath.sync="catelogPath"></category-cascader>
      </el-form-item>

      <el-form-item label="所属分组" prop="attrGroupId" v-if="type == 1">
        <el-select
          ref="groupSelect"
          v-model="dataForm.attrGroupId"
          placeholder="请选择"
        >
          <el-option
            v-for="item in attrGroups"
            :key="item.attrGroupId"
            :label="item.attrGroupName"
            :value="item.attrGroupId"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="是否需要检索" prop="searchType">
        <!-- <el-input v-model="dataForm.searchType" placeholder="是否需要检索[0-不需要，1-需要]"></el-input> -->
        <el-switch
          v-model="dataForm.searchType"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>

      <el-form-item label="启用状态" prop="enable">
        <!-- <el-input v-model="dataForm.enable" placeholder="启用状态[0 - 禁用，1 - 启用]"></el-input> -->
        <el-switch
          v-model="dataForm.enable"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="0"
          :inactive-value="1"
        ></el-switch>
      </el-form-item>

      <el-form-item label="快速展示" prop="showDesc">
        <!-- <el-input v-model="dataForm.showDesc" placeholder="快速展示【是否展示在介绍上；0-否 1-是】，在sku中仍然可以调整"></el-input> -->
        <el-switch
          v-model="dataForm.showDesc"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="0"
          :inactive-value="1"
        ></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import CategoryCascader from "../common/category-cascader.vue";
import { getAttrById, saveAttr, updateAttr } from "../../../api/product/attr";
import { getAttrGroupList } from "../../../api/product/attrgroup";
export default {
  components: {
    CategoryCascader,
  },
  data() {
    return {
      visible: false,
      result: "",
      dataForm: {
        attrId: 0,
        attrName: "",
        // 是否需要检索[0-不需要，1-需要]
        searchType: 0,
        icon: "",
        valueSelect: "",
        // 属性类型[0-销售属性，1-基本属性，2-既是销售属性又是基本属性]
        attrType: 1,
        //启用状态[0 - 禁用，1 - 启用]
        enable: 0,
        catelogId: "",
        // 快速展示【是否展示在介绍上；0-否 1-是】，在sku中仍然可以调整
        showDesc: 0,
        attrGroupId: "",
      },
      valueType: 1,
      catelogPath: [],
      attrGroups: [],
      dataRule: {
        attrName: [
          { required: true, message: "属性名不能为空", trigger: "blur" },
        ],
        searchType: [
          {
            required: true,
            message: "是否需要检索[0-不需要，1-需要]不能为空",
            trigger: "blur",
          },
        ],
        icon: [
          { required: true, message: "属性图标不能为空", trigger: "blur" },
        ],
        valueSelect: [
          {
            required: true,
            message: "可选值列表[用逗号分隔]不能为空",
            trigger: "blur",
          },
        ],
        attrType: [
          {
            required: true,
            message:
              "属性类型[0-销售属性，1-基本属性，2-既是销售属性又是基本属性]不能为空",
            trigger: "blur",
          },
        ],
        enable: [
          {
            required: true,
            message: "启用状态[0 - 禁用，1 - 启用]不能为空",
            trigger: "blur",
          },
        ],
        catelogId: [
          { required: true, message: "所属分类不能为空", trigger: "blur" },
        ],
        showDesc: [
          {
            required: true,
            message:
              "快速展示【是否展示在介绍上；0-否 1-是】，在sku中仍然可以调整不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  props: {
    type: {
      type: Number,
      default: 1,
    },
  },
  watch: {
    catelogPath(path) {
      console.log("路径变了", path);
      this.attrGroups = [];
      this.dataForm.attrGroupId = "";
      this.dataForm.catelogId = path[path.length - 1];
      if (path && path.length == 3) {
        getAttrGroupList(
          this.$http.adornParams({ page: 1, limit: 10 }),
          this.dataForm.catelogId
        ).then(({ data }) => {
          console.log("catelogPath----->")
          console.log(data);
          if (data && data.code === 200) {
            this.attrGroups = data.data.list;
          } else {
            this.$message.error(data.msg);
          }
        });
      } else if (path.length == 0) {
        this.dataForm.catelogId = "";
      } else {
        this.$message.error("请选择正确的分类");
        this.dataForm.catelogId = "";
      }
    },
  },
  methods: {
    init(id) {
      this.dataForm.attrId = id || 0;
      this.visible = true;
      this.dataForm.attrType = this.type
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.attrId) {
          getAttrById(this.dataForm.attrId).then(({ data }) => {
            console.log("init----->")
            console.log(data)
            if (data && data.code === 200) {
              this.dataForm.attrName = data.data.attrName;
              this.dataForm.searchType = data.data.searchType;
              this.dataForm.icon = data.data.icon;
              this.dataForm.valueSelect = data.data.valueSelect.split(";");
              this.dataForm.attrType = data.data.attrType;
              this.dataForm.enable = data.data.enable;
              this.dataForm.catelogId = data.data.catelogId;
              this.dataForm.showDesc = data.data.showDesc;
              //分类
              this.catelogPath = data.data.catelogPath
              //分组
              this.$nextTick(() => {
                this.dataForm.attrGroupId = data.data.attrGroupId;
              });
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (this.dataForm.attrId) {
            console.log("更新")
            this.dataForm.attrType = this.type //属性状态值，由子组件传来
            this.dataForm.valueSelect = this.dataForm.valueSelect.join(";")
            const updateResult = updateAttr(this.dataForm);
            this.result = updateResult;
          } else {
            console.log("保存")
            this.dataForm.attrType = this.type
            this.dataForm.valueSelect = this.dataForm.valueSelect.join(";")
            const saveResult = saveAttr(this.dataForm);
            this.result = saveResult;
          }
          this.result.then(({ data }) => {
            console.log("-------------")
            console.log(data)
            if (data && data.code === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    dialogClose() {
      this.catelogPath = [];
    },
  },
};
</script>